import api from "../../../http/api";
export default {
	data() {
		return {
			// 我的地址 (固定值)
			myAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
			formData: {
				toAddress: '',
				accountNumber: '',
				password: ''
			},
			errors: {
				toAddress: '',
				accountNumber: '',
				password: ''
			},
			tabs: [
			  { title: '代收' },
			  { title: '代付' }
			],
			activeTab: 0, // 当前激活的选项卡索引
			sliderStyle: {
			  width: '50%',
			  transform: 'translateX(0%)'
			},
			collectItems: [
			  { type: '支付宝转账小额', range: '100-1000', rate: '3', maxRate: 3,img:'/static/payImg/weixin.png' },
			  { type: '支付宝转账大额', range: '1000-500000', rate: '0', maxRate: 0 ,img:'/static/payImg/dingding.png' },
			  { type: '银行卡转账', range: '100-1000000', rate: '3', maxRate: 3 ,img:'/static/payImg/shuzirenmingbi.png' },
			  { type: '数字人民币', range: '100-10000', rate: '0', maxRate: 0 ,img:'/static/payImg/yinghangkazhuangzhang.png' },
			  { type: '微信', range: '100-100000', rate: '0', maxRate: 0 ,img:'/static/payImg/weixin.png' },
			  { type: '支付宝内层', range: '1000-50000', rate: '0', maxRate: 0 ,img:'/static/payImg/yunshanfu.png' },
			  { type: '支付宝扫码内层', range: '1000-50000', rate: '2', maxRate: 2 ,img:'/static/payImg/weixin.png' },
			  { type: '支付寶内层', range: '1000-30000', rate: '2', maxRate: 2 ,img:'/static/payImg/weixin.png' },
			  { type: '支付宝固额1', range: '945-945', rate: '0', maxRate: 0 ,img:'/static/payImg/zhifubao.png' }
			],
			payItems: [
			  { type: '支付宝代付', range: '1-50000', rate: '0', maxRate: 0 }
			],
			
			template:1,//1当前版，2隐藏版
			collectDivide:[],//代收比例
			payDivide:{},//代付比例
			
		}
	},
	onLoad() {
		this.loadData(); // 首次加载数据
	},
	methods: {
		loadData(){
			let that = this;
			api.commissionRebateRatio({}).then(res=>{
				if(res.code==1){
					this.collectDivide = res.data.bank_rebate;
					this.collectDivide.forEach(item=>{
						item.rate = ''
					})
					this.payDivide = {
						payment_rebate:res.data.payment_rebate,
						bank_type_id:3,
						rate:''
					}
				}else{
					uni.showToast({ title: res.msg, icon: 'none' });
				}
			}).catch(err=>{
				
			})
		},
		//输入触发
		chInput(){
			console.log(this.collectDivide)
		},
		// 复制地址功能
		copyAddress() {
			uni.setClipboardData({
				data: this.myAddress,
				success: () => {
					uni.showToast({
						title: '地址已复制',
						icon: 'success'
					});
				}
			});
		},
		
		// 验证转账地址
		validateAddress() {
			const accountNumber = this.formData.accountNumber;
			if (!accountNumber) {
				this.errors.accountNumber = '代理人账号不能为空';
			}else {
				this.errors.accountNumber = '';
			}
		},
		// 验证交易密码
		validatePassword() {
			const password = this.formData.password;
			if (!password) {
				this.errors.password = '密码不能为空';
			} else {
				this.errors.password = '';
			}
		},
		//表单验证
		saveSettings() {
			if(!this.formData.accountNumber){
				uni.showToast({ title: '代理人账号不能为空', icon: 'none' });
				return
			}
			if(!this.formData.password){
				uni.showToast({ title: '密码不能为空', icon: 'none' });
				return
			}
			// 验证所有输入值是否有效
			// let isValid = true;
			// const items = this.activeTab === 'collect' ? this.collectItems : this.payItems;

			// for (const item of items) {
			// 	if (item.rate === '') {
			// 		uni.showToast({
			// 			title: '请填写所有返佣比例',
			// 			icon: 'none'
			// 		});
			// 		isValid = false;
			// 		break;
			// 	}

			// 	const rate = parseInt(item.rate);
			// 	/*  */

			// 	if (rate < 0 || rate > item.maxRate) {
			// 		uni.showToast({
			// 			title: `返佣比例必须在0-${item.maxRate}%之间`,
			// 			icon: 'none'
			// 		});
			// 		isValid = false;
			// 		break;
			// 	}
			// }

			api.createAgency({
				username:this.formData.accountNumber,
				password:this.formData.password,
				bank_type_1:this.collectDivide[0].rate,
				bank_type_2:this.collectDivide[1].rate,
				bank_type_3:this.collectDivide[2].rate,
				bank_type_4:this.collectDivide[3].rate,
				bank_type_5:this.collectDivide[4].rate,
				bank_type_6:this.collectDivide[5].rate,
				payment_rebate:this.payDivide.rate
			}).then(res=>{
				if(res.code==1){
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					});
					setTimeout(()=>{
						uni.navigateBack({delta:1})
					},1500)
				}else{
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				}
			}).catch(err=>{
				
			})
		},
		switchTab(index) {
		  this.activeTab = index;
		  
		  // 计算滑动指示器的位置
		  const translateX = index * 100;
		  this.sliderStyle = {
		    width: `${100 / this.tabs.length}%`,
		    transform: `translateX(${translateX}%)`,
		    transition: 'transform 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55)'
		  };
		},
		/* 过滤器start */
		//收款渠道
		paytype(value){
			if(value==1){
				return '银行卡'
			}else if(value==2){
				return '微信'
			}else if(value==3){
				return '支付宝'
			}else if(value==4){
				return '云闪付'
			}else if(value==5){
				return '数字人民币'
			}else if(value==6){
				return '钉钉'
			}
		},
		//收款渠道图片
		payimg(value){
			if(value==1){
				return '/static/payImg/yinghangkazhuangzhang.png'
			}else if(value==2){
				return '/static/payImg/weixin.png'
			}else if(value==3){
				return '/static/payImg/zhifubao.png'
			}else if(value==4){
				return '/static/payImg/yunshanfu.png'
			}else if(value==5){
				return '/static/payImg/shuzirenmingbi.png'
			}else if(value==6){
				return '/static/payImg/dingding.png'
			}
		},
		//订单状态
		orderStart(value){
			if(value==0){
				return '待支付'
			}else if(value==1){
				return '已抢单'
			}else if(value==2){
				return '已支付'
			}else if(value==3){
				return '已完成'
			}else if(value==4){
				return '已取消'
			}
		},
		//时间戳
		timeString(value){
			// 1. 转换时间戳
			const timestamp = value;
			const millis = timestamp * 1000;
			
			// 2. 格式化为字符串"2025-07-18 17:59:55" (北京时间)
			const dateStr = new Date(millis).Format("yyyy-MM-dd hh:mm:ss");
			return dateStr
			
			// 3. 使用自定义格式 "2025年07月18日 17时59分"
			// const customFormat = new Date(millis).Format("yyyy年MM月dd日 hh时mm分");
		},
		/* 过滤器end */
	},
	computed: {
		isFormValid() {
			return Object.values(this.errors).every(error => !error) &&
				Object.values(this.formData).every(val => val !== '');
		}
	},
	mounted() {
	  // 初始化滑块位置
	  this.sliderStyle = {
	    width: `${100 / this.tabs.length}%`,
	    transform: `translateX(${this.activeTab * 100}%)`
	  };
	}
}